<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Env;
use app\facade\Vproduct;
use think\facade\Validate;

// use app\admin\Model\Gkdata;
class Product extends Base
{
    // 搜索名称查商品
	public function search(){
        $request=new request();
        $sname=$request->post('sname');
        $data=Db::table('product')->where('pname','like','%'.$sname.'%')->paginate(5);
        $typedata=Db::name('type')->select();
        $this->assign('typedata',$typedata);
        $this->assign('data',$data);
        return $this->fetch('index');
    }

    // 分类查商品
	public function search1(){
    $request=new request();
    $tid=$request->post('tid');
    $data=Db::table('product')->where('tid',$tid)->paginate(5);
    $typedata=Db::name('type')->select();
    $this->assign('typedata',$typedata);
    $this->assign('data',$data);
    return $this->fetch('index');
    }
    public function index()
    {
        $data=Db::table('product')->order("pid","DESC")->paginate(5);
        $typedata=Db::name('type')->select();
        $this->assign('typedata',$typedata);
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 进入添加商品页面
    public function addpage()
    {
        $data=Db::table('type')->select();
        $yhtype=Db::table('yhtype')->select();
        $ptzq=Db::table('ptzq')->select();
        $this->assign('ptzq',$ptzq);
        $this->assign('data',$data);
        $this->assign('yhtype',$yhtype);
        
        return $this->fetch();
    }
    // 添加商品
    public function addpro()
    {
        $request=new request();
        $tid=$request->post('tid');
        $yid=$request->post('yid');
        $pt=$request->post('pt');
        $pname=$request->post('pname');
        $pnum=$request->post('pnum');
        $poldprice=$request->post('poldprice');
        $pnewprice=$request->post('pnewprice');
        $yongj=$request->post('yongj');
        $pdetail=$request->post('pdetail');
        $grade=$request->post('grade');
        
        // $grade1=$request->post('grade1');
        // $grade1p=$request->post('grade1p');
        // $grade2=$request->post('grade2');
        // $grade2p=$request->post('grade2p');
        // $grade3=$request->post('grade3');
        // $grade3p=$request->post('grade3p');
        // $grade4=$request->post('grade4');
        // $grade4p=$request->post('grade4p');
        // $grade5=$request->post('grade5');
        // $grade5p=$request->post('grade5p');
        // $salenum=$request->post('salenum');
        // 实例化上传图片的对象
        $upimgobj=controller('Common');
        // $image_error = $_FILES['pthumb']['error'];
        // 需要先用$request()->file('')判断是否为空，否则$request->file('pthumb')没上传图片时会报错
        $file = $request->file('');
        if (empty($file['pthumb'])) {
            $pthumb='';
        }else{
            $file1=$request->file('pthumb');
            // 为什么不执行下行代码，因为php上传文件后较短时间会删除临时文件，也就是$_FILES[‘file’][‘tmp_name’]这个值，这样我下方的fiel('bnnerimg')就会找不到临时文件，然后报错。真的是日了狗
            // $thumb=$upimgobj->upaction1($file);          
        }
        if (empty($file['bannerimg'])) {
            $bannerimg='';
        }else{
            $file2=$request->file('bannerimg');
            // $bannerimg=$upimgobj->upaction1($file2);
        }
        if(!empty($file1)){           
            $pthumb=$upimgobj->upaction1($file1);
        }
        $img1='';
        $img2='';
        $img3='';
        $img4='';
        $img5='';
        // dump($file2) ;exit;
        if(!empty($file2)){
            $bannerimg=$upimgobj->upaction2($file2);
            // dump($bannerimg);
            foreach($bannerimg as $key=>$val){
            if($key==0){
                $img1=$val;
            }
            if($key==1){
                $img2=$val;
            }
            if($key==2){
                $img3=$val;
            }
            if($key==3){
                $img4=$val;
            }
            if($key==4){
                $img5=$val;
            }
        }
        }
        $status=$request->post('status');
        $data=[
            "tid"=>$tid,
            "yid"=>$yid,
            "pt"=>$pt,
            "pname"=>$pname,
            "pnum"=>$pnum,
            "poldprice"=>$poldprice,
            "pnewprice"=>$pnewprice,
            "yongj"=>$yongj,
            "pdetail"=>$pdetail,
            "pthumb"=>$pthumb,
            "img1"=>$img1,
            "img2"=>$img2,
            "img3"=>$img3,
            "img4"=>$img4,
            "img5"=>$img5,
            "status"=>$status,
            "grade"=>$grade,
            "time"=>time()
        ];
        // 需要验证的字段
        $checkdata=[
            "pname"=>$pname,
            "pnum"=>$pnum,
            "poldprice"=>$poldprice,
            "pnewprice"=>$pnewprice,
            "yongj"=>$yongj,
            ];
        if(!Vproduct::check($checkdata)){
			return Vproduct::getError();
		}else{
            $re=Db::name('product')->insert($data);
            $pid=Db::name('product')->getLastInsID();
            if($re){
                if($pt=="1"){
                    $this->success('新增成功,正在跳转添加拼团信息', 'ptzq/addpage?pid='.$pid.'&pname='.$pname.'&grade='.$grade);
                }else if($grade=="1"){
                    $this->success('新增成功,正在跳转添加属性等级信息', 'psx/addpage?pid='.$pid.'&pname='.$pname);
                }else{
                    $this->success('新增成功', 'product/index');
                }
            }else{
                return $this->error('插入失败');
            }
        }
    }
    
    // 修改启用状态
    public function stchangeproduct(){
        $request=new request();
        $pid= intval($request->get("id"));
        $status=intval($request->get("status"));
        if($status==0){
            $status=1;
        }else{
            $status=0;
        }
        $data=[
            "pid"=>$pid,
            "status"=>$status
        ];
        $da=Db::name('product')->update($data);
        if($da){
            $this->success('状态修改成功', 'product/index');
        }
        else{
            return '更新失败'.$da;
        }
    }
    // 进入修改产品页面
    public function editpage(){
        $request=new request();
        $pid= intval($request->get("id"));
        $data=Db::name('product')->where('pid',$pid)->find();
        $yhtype=Db::table('yhtype')->select();
        $typedata=Db::name('type')->select();
        $ptzq=Db::table('ptzq')->select();
        $this->assign('ptzq',$ptzq);
        $this->assign('data',$data);
        $this->assign('typedata',$typedata);
        $this->assign('yhtype',$yhtype);
        return $this->fetch();
    }
    // 修改产品内容
    public function editpro()
    {
        $request=new request();
        $data=[];
        $pid=$request->post('pid');
        $data['pid']=$pid;
        // 查找该产品并对照表单数据
        $data1=Db::table('product')->where('pid',$pid)->find(); 
        $tid=$request->post('tid');
        if($tid != $data1['tid']){
            $data['tid']=$tid;
        }
        $yid=$request->post('yid');
        if($yid != $data1['yid']){
            $data['yid']=$yid;
        }
        $ptid=$request->post('ptid');
        $pt=$request->post('pt');
        if($pt != $data1['pt']){
            $data['pt']=$pt;
        }
        $pname=$request->post('pname');
        if($pname != $data1['pname']){
            $data['pname']=$pname;
        }
        $pnum=$request->post('pnum');
        if($pnum != $data1['pnum']){
            $data['pnum']=$pnum;
        }
        $poldprice=$request->post('poldprice');
        if($poldprice != $data1['poldprice']){
            $data['poldprice']=$poldprice;
        }
        $pnewprice=$request->post('pnewprice');
        if($pnewprice != $data1['pnewprice']){
            $data['pnewprice']=$pnewprice;
        }
        $yongj=$request->post('yongj');
        if($yongj != $data1['yongj']){
            $data['yongj']=$yongj;
        }
        $grade=$request->post('grade');
        if($grade != $data1['grade']){
            $data['grade']=$grade;
        }
        // 实例化上传图片的对象
        $upimgobj=controller('Common');
        $file = $request->file('');
        if (!empty($file['pthumb'])) {
            $file1=$request->file('pthumb');
        }
        // $image_error2 = $_FILES['bannerimg']['error'];
        if (!empty($file['bannerimg'])) {
            $file2=$request->file('bannerimg');
        }
        
        if(!empty($file1)){
            // dump($file);exit;
            $pthumb=$upimgobj->upaction1($file1);
            // dump($pthumb);exit;
            $data['pthumb']=$pthumb;
        }
        if(!empty($file2)){
            $bannerimg=$upimgobj->upaction2($file2);
            // dump($bannerimg);
            foreach($bannerimg as $key=>$val){
                if($key==0){
                    $data['img1']=$val;
                }
                if($key==1){
                    $data['img2']=$val;
                }
                if($key==2){
                    $data['img3']=$val;
                }
                if($key==3){
                    $data['img4']=$val;
                }
                if($key==4){
                    $data['img5']=$val;
                }
            }
        }

        $pdetail=$request->post('pdetail');
        if($pdetail != $data1['pdetail']){
            $data['pdetail']=$pdetail;
        }
        $status=$request->post('status');
        if($status != $data1['status']){
            $data['status']=$status;
        }
        // var_dump($data);
        $da=Db::name('product')->update($data);
        if($da){
            if($pt==1&&$ptid==0){
                $this->success('修改成功,正在跳转添加拼团信息', 'ptzq/addpage?pid='.$pid.'&pname='.$pname.'&grade='.$grade);
            }else if($grade=="1"&&$data1['grade']=="0"){
                $this->success('修改成功,正在跳转添加属性等级信息', 'psx/addpage?pid='.$pid.'&pname='.$pname);
            }else{
                $this->success('修改成功', 'product/index');
            }
        }
        else{
            return $this->error('更新失败');
        }

    }


    // 进入产品类型列表页面
    public function typeindex(){
        $data=Db::table('type')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 进入添加产品类型页面
    public function ntype()
    {
        $data=Db::table('type')->select();
        $this->assign('data',$data);
        return $this->fetch('addtype');
    }
    // 添加产品类型
    public function addtype()
    {
        $request=new request();
        $ntname=$request->post("title");
        $topid=$request->post("topid");
        
        $upimgobj=controller('Common');
        // var_dump($_FILES);
        if ($_FILES['tyimg']['error']) {
            $tyimg='';
        }else{
            $file=$request->file('tyimg');
        }
        if(!empty($file)){           
            $tyimg=$upimgobj->upaction1($file);
        }
        if(empty($topid)){
            $topid=-1;
        }else{
            $topid=(int)$topid;
        }
        $data=[
        "tname"=>$ntname,
        "topid"=>$topid,
        "tyimg"=>$tyimg
        ];
        $re=Db::name('type')->insert($data);
        if($re){
            $this->success('新增成功', 'Product/typeindex');
        }else{
            return $this->error('插入失败');
        }
    }
    // 进入更改产品类型页面
    public function edtype()
    {
        $request=new request();
        $tid=$request->get("id");
        $data=Db::name('type')->where('tid',$tid)->find();
        $topdata=Db::name('type')->where('topid','eq',0)->where('tid','neq',$tid)->select();
        $this->assign('data',$data);
        $this->assign('topdata',$topdata);
        return $this->fetch();

    }
    //更改产品类型 
    public function edittype()
    {
        
        $request=new request();
        $tid=$request->post("tid");
        $topid=$request->post("topid");
        $tname=$request->post("tname");
        
        $data1=Db::table('type')->where('tid',$tid)->find(); 
        $data['tid']=$tid;
        if($tname != $data1['tname']){
            $data['tname']=$tname;
        }
        if($topid != $data1['topid']){
            $data['topid']=$topid;
        }
        
         $upimgobj=controller('Common');
        // var_dump($_FILES);
        if ($_FILES['tyimg']['error']) {
            $tyimg='';
        }else{
            $file=$request->file('tyimg');
        }
        if(!empty($file)){           
            $tyimg=$upimgobj->upaction1($file);
            $data['tyimg']=$tyimg;
        }
        $da=Db::name('type')->update($data);
        if($da){
            $this->success('修改成功', 'product/typeindex');
        }
        else{
            return $this->error('更新失败');
        }
    }  
     //删除产品类型 
    public function deltype()
    {
        $request=new request();
        $tid=$request->get("id");
        $da=Db::name('type')->delete($tid);
        if($da){
            $this->success('删除成功', 'product/typeindex');
        }
        else{
            return $this->error('删除失败');
        }
    }
     //删除产品
    public function delproduct()
    {
        $request=new request();
        $pid=$request->get("id");
        $da=Db::name('product')->delete($pid);
        if($da){
            $this->success('删除成功', 'product/index');
        }
        else{
            return $this->error('删除失败');
        }
    }
}
